package com.bro.person.admin.service.impl;

import com.bro.person.base.model.paging.PagingResult;
import com.bro.person.base.service.impl.BroBaseServiceImpl;
import com.bro.person.admin.dao.mapper.AdminUserMapper;
import com.bro.person.admin.model.AdminUser;
import com.bro.person.admin.model.AdminUserCriteria;
import com.bro.person.admin.service.IAdminUserService;

import com.bro.person.base.constants.ConstConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * AdminUser的 DB操作service
 *
 * @author jdd
 * @version 1.0
 * @date 2018-10-11 09:53:41
 */
@Service
@Transactional(rollbackFor={Exception.class})
public class AdminUserServiceImpl extends BroBaseServiceImpl<AdminUser, AdminUserCriteria, Integer> implements IAdminUserService {

	private static Logger logger = LoggerFactory.getLogger(AdminUserServiceImpl.class);

	@Resource
	@SuppressWarnings("SpringJavaAutowiringInspection")
	private AdminUserMapper adminUserMapper;

	@Override
	public AdminUserMapper getBaseMapper() {
		return adminUserMapper;
	}

	@Override
	public List<AdminUser> queryAll(String orderByClause) {
		logger.debug("[AdminUserServiceImpl][queryAll]entering, orderByClause:{}", orderByClause);
		AdminUserCriteria criteria = new AdminUserCriteria();
		criteria.createCriteria();
		criteria.setOrderByClause(orderByClause);
		return queryByCriteria(criteria);
	}

	@Override
	public List<AdminUser> fallloadByCriteria(int pageSize, AdminUserCriteria criteria) {
		logger.debug("[AdminUserServiceImpl][fallloadByCriteria]entering, pageSize:{}", pageSize);
		return null;
	}

	@Override
	public PagingResult<AdminUser> pagingByCriteria(int pageNo, int pageSize, AdminUserCriteria criteria, AdminUser adminUser) {
		logger.debug("[AdminUserServiceImpl][pagingByCriteria] entering, pageNo:{},pageSize:{}", pageNo, pageSize);
		// 确保pageNo合法
		pageNo = pageNo <= 0 ? 1 : pageNo;
		pageSize = pageSize <= 0 ? ConstConfig.PAGE_SIZE_DEFAULT : pageSize;
		// 确保pageSize合法
		int offset = (pageNo - 1) * pageSize;

		// 构造查询条件
		if (criteria == null) {
			criteria = new AdminUserCriteria();
		}

		criteria.setLimitOffset(offset);
		criteria.setLimitRows(pageSize);
		logger.info("[AdminUserServiceImpl][pagingByCriteria] offset:{}, pageNo: {},pageSize:{}", offset, pageNo, pageSize);
		int count = super.countByCriteria(criteria);
		List<AdminUser> dataList = adminUserMapper.selectByExample(criteria);
		// 返回分页数据
		logger.info("[AdminUserServiceImpl][pagingByCriteria] result, pageNo: {},pageSize:{},count:{},dataListSize:{}",
				pageNo, pageSize, count, dataList != null ? dataList.size() : 0);
		return new PagingResult<>(pageNo, pageSize, count, dataList);
	}

	/**
	 * 通过username 查询用户信息（从cache中查询）
	 * @param username
	 * @return
	 */
	@Override
	public AdminUser loadCacheByUsername(String username) {
		//new 一个对象 防止缓存击穿
		AdminUser adminUser = new AdminUser();
		//从cache中查询数据 如果没有从数据库中查询
		AdminUserCriteria adminUserCriteria = new AdminUserCriteria();
		AdminUserCriteria.Criteria criteria = adminUserCriteria.createCriteria();
		criteria.andUsernameEqualTo(username);
		List<AdminUser> adminUserList = adminUserMapper.selectByExample(adminUserCriteria);
		if(adminUserList!=null && adminUserList.size()>0 && adminUserList.get(0)!=null){
			//放入到缓存中
			adminUser = adminUserList.get(0);
		}
		return adminUser;
	}
}